package part1.q1;

import java.util.Stack;

public class MyStack2 {
	
	
	
	
	private Stack<Integer> stackData;
	private Stack<Integer> stackMin;
	
	public MyStack2() {
		stackData = new Stack<Integer>();
		stackMin = new Stack<Integer>();
	}
	
	
	public void push(int newNum) {
		if(stackMin.isEmpty()) {
			stackMin.push(newNum);
		} else if(newNum < getMin()) {
			stackData.push(newNum);
		} else {
			int newMin = stackMin.peek();
			this.stackMin.push(newMin);
		}
		this.stackData.push(newNum);
	}
	
	public int pop() {
		if(this.stackData.isEmpty()) {
			throw new RuntimeException("Your stack is empty.");
		}
		this.stackMin.pop();
		return this.stackData.pop();
	}
	
	public int getMin() {
		if(stackMin.isEmpty()) {
			throw new RuntimeException("Your stack is empty.");
		}
		return stackMin.peek();
	}
	
	
	
	
	
	public static void main(String[] args) {
	    MyStack2 myStack2 = new MyStack2();
		myStack2.push(3);
		myStack2.push(4);
		myStack2.push(5);
		myStack2.push(1);
		myStack2.push(2);
		myStack2.push(1);
		System.out.println( "min : " + myStack2.getMin());
		System.out.println(myStack2.pop());
		System.out.println(myStack2.pop());
		System.out.println(myStack2.pop());
		
		System.out.println("------");
		System.out.println("min : " + myStack2.getMin());
	}
	
	
	
	
}
